<?php

class Application_Model_Parceria extends Zend_Db_Table_Abstract {

    protected $_name = 'parceria';
    protected $_primary = 'codigo';
    private static $instance;

    public static function getInstance() {
        if (!isset(self::$instance)) {
            self::$instance = new Application_Model_Parceria();
            return self::$instance;
        }
        return self::$instance;
    }

    public function insert($data) {
        if (isset($data['descricao']) && isset($data['valor']) && isset($data['dt_cadastro'])
                && isset($data['dt_expiracao'])) {
            parent::insert($data);
        }
    }

    public function update(array $data, $where = '') {
        $where = 'codigo = ' . $data['codigo'];
        parent::update($data, $where);
    }

    public function getById($id) {
        return $this->fetchRow("codigo = ". $id ." and dt_exclusao is null");
    }

    public function lista($page, $qntPagina, $orderName, $order,$data) {
        $sql = 'select codigo,descricao,dt_cadastro,dt_expiracao,valor 
                from parceria
                where dt_exclusao is null ';

        if(isset($data['descricao']) && !empty($data['descricao'])){
            $sql.=' and descricao like \''. $data['descricao']. '%\'';
        }
        
        if(isset($data['dt_cadastro']) && !empty($data['dt_cadastro'])){
            $sql.=' and dt_cadastro = \''. $data['dt_cadastro']. '\'';
        }
        
        if(isset($data['dt_expiracao']) && !empty($data['dt_expiracao'])){
            $sql.=' and dt_expiracao = \''. $data['dt_expiracao']. '\'';
        }
        
        if (isset($orderName) && isset($order)) {
            $sql .= ' order by ' . $orderName . ' ' . $order;
        }
       
        $rs = $this->getAdapter()->query($sql)->fetchAll();
        $paginator = Zend_Paginator::factory($rs);
        $paginator->setCurrentPageNumber($page);
        $paginator->setItemCountPerPage($qntPagina);

        return $paginator;
    }

    public function listaCombo(){
        $sql =' select codigo,descricao from parceria where dt_exclusao is null and dt_expiracao >= \''. Zend_Date::now()->get('Y-M-d'). '\'';
        return $this->getAdapter()->query($sql)->fetchAll();
    }
    
    public function remove($id){
        $parceria = $this->getById($id)->toArray();
        $where = ' codigo = ' .$id;
        $parceria['dt_exclusao'] = Zend_Date::now()->get('Y/M/d');
        parent::update($parceria, $where);
    }
    
    
}